package sz.szsmk.citizencard.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chenenyu.router.annotation.Route;

import org.greenrobot.eventbus.EventBus;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.IdCardAuthenticationActivityBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.CheckIsChargeRes;
import sz.szsmk.citizencard.model.network.bean.response.FirstSearchUserLevelRes;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.model.network.dzbean.req.PreAuthThirdRes;
import sz.szsmk.citizencard.model.network.dzbean.res.CheckIsRealName2Res;
import sz.szsmk.citizencard.model.network.dzbean.res.CheckIsRealNameRes;
import sz.szsmk.citizencard.model.network.dzbean.res.GetOpenIdRes;
import sz.szsmk.citizencard.model.network.dzbean.res.QueryDzsmkAgreeMentRes;
import sz.szsmk.citizencard.mvp.contract.IRealNameContact;
import sz.szsmk.citizencard.mvp.presenter.RealNamePresenter;
import sz.szsmk.citizencard.ui.adapter.bean.AppHomeCardBean;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.ButtonUtils;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.MesageEventBus;
import sz.szsmk.citizencard.util.StringUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/24 15:23
 * description: 实名认证
 */
@Route("IDCardAuthenticationActivity")
public class IDCardAuthenticationActivity extends IBaseActivity<RealNamePresenter, IdCardAuthenticationActivityBinding>
        implements IRealNameContact.IRealNameView, View.OnClickListener {

    CheckBox checkbox;
    TextView checkTxt;
    EditText name;
    EditText idCard;
    private int intoType = 0;
    private String chooseType = "";

    @Override
    protected int attachLayoutRes() {
        return R.layout.id_card_authentication_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent()).build().inject(this);
    }

    @Override
    protected void initViews() {

        checkbox = findViewById(R.id.id_card_checkbox);
        checkTxt= findViewById(R.id.check_txt);
        name = findViewById(R.id.name);
        idCard= findViewById(R.id.id_card);

        findViewById(R.id.submit).setOnClickListener(this);


        dataBinding.titleLayout.settingTitle.setText("L2认证");
        dataBinding.titleLayout.back.setOnClickListener(this);
        intoType = getIntent().getIntExtra("intotype", 0);
        chooseType = getIntent().getStringExtra("choosetype");
        SpannableString spannableString = new SpannableString("我已阅读并同意《智慧苏州用户实名认证服务协议》");
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#FF00AAFF"));
        spannableString.setSpan(colorSpan, 8, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        checkTxt.setText(spannableString);
    }

    @Override
    protected void updateViews() {

    }

    public void click1(View v) {
        if (v.getId() == R.id.submit) {
            if (StringUtils.isBlank(name.getText().toString())) {
                Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
                return;
            }
            if (StringUtils.isBlank(idCard.getText().toString())) {
                Toast.makeText(this, "请输入身份证号", Toast.LENGTH_SHORT).show();
                return;
            }
            InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
            }
            mPresenter.realNameCertification(name.getText().toString(), idCard.getText().toString());
        }
    }

    @Override
    public void firstSearchUserLevelSucc(AppHomeCardBean response) {

    }

    @Override
    public void firstSearchUserLevelFail(FirstSearchUserLevelRes response) {
        Toast.makeText(this, response.getMessage(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void realNameCertificationSucc() {
        Toast.makeText(this, "L2级实名认证已完成\n您的实名卡信息在\"我的市民卡\"可查", Toast.LENGTH_LONG).show();
        if (intoType == 1) {
            Bundle bundle = new Bundle();
            bundle.putString(Constant.ID_CARD, idCard.getText().toString());
            bundle.putString(Constant.CUST_NAME, name.getText().toString());
            bundle.putInt("intotype", intoType);
            bundle.putString("choosetype", chooseType);
            MesageEventBus mesageEventBus = new MesageEventBus();
            mesageEventBus.setFaceName(name.getText().toString());
            EventBus.getDefault().postSticky(mesageEventBus);
            JumpUtils.jumptoNatvie(this, "FaceAuthActivity", 0, bundle);
        }

        finish();
    }

    @Override
    public void realNameCertificationFail(IbaseBean response) {
        Toast.makeText(this, response.getMessage(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void getOpenIdFail(GetOpenIdRes response) {

    }

    @Override
    public void getOpenIdSucc(GetOpenIdRes response) {

    }

    @Override
    public void queryDzsmkAgreementFail(QueryDzsmkAgreeMentRes response) {

    }

    @Override
    public void queryDzsmkAgreementSucc(QueryDzsmkAgreeMentRes response, String openId) {

    }

    @Override
    public void preAuthSucc(PreAuthThirdRes preAuthRes) {

    }

    @Override
    public void preAuthFail() {

    }

    @Override
    public void checkIsOpenElecCardFail(CheckIsRealNameRes preAuthRes) {

    }

    @Override
    public void checkIsOpenElecCardSucc(CheckIsRealNameRes preAuthRes) {

    }

    @Override
    public void checkIsOpenElecCardSucc2(CheckIsRealName2Res preAuthRes) {

    }

    @Override
    public void checkIsChargeFail() {

    }

    @Override
    public void checkIsChargeSucc(CheckIsChargeRes ibaseBean) {

    }

    @Override
    public void getAliStringSuccess(String st) {

    }

    @Override
    public void getAlipayUserInfo(BaseBean bean) {

    }

    @Override
    public void getAlipayUserInfoFail(BaseBean bean) {

    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back) {
            finish();
        } else if (id == R.id.submit) {
            click1(v);
        }
    }
}
